import type { OptionTypes, ResponseData } from '@/utils/typings.d';
import { SUCCESS_CODE } from '@/utils/typings.d';
import { request } from 'umi';
import type {
  CreateDingRobotType,
  UpdateDingRobotType,
  DingRobotsTableItem,
  SystemWarningRuleTableItem,
  CreateSystemWarningRule,
  UpdateSystemWarningRule,
  SystemWarningHistroyTableItem,
} from './typings.d';

export async function queryDingRobots(params: any, sort: any) {
  const res: ResponseData<{
    total: number;
    list: DingRobotsTableItem[];
  }> = await request('/api/system-warning/v1.0.0/robots', {
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.list };
  return { success: false, total: 0, data: [] };
}

export async function createDingRobot(params: CreateDingRobotType) {
  const res: ResponseData<null> = await request('/api/system-warning/v1.0.0/robot', {
    method: 'POST',
    data: params,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '添加成功',
    };
  return {
    data: res?.data,
    success: false,
    message: res?.message || '添加失败',
  };
}

export async function updateDingRobot(id: string, newInfo: UpdateDingRobotType) {
  const res: ResponseData<null> = await request(`/api/system-warning/v1.0.0/robot/${id}`, {
    method: 'PUT',
    data: newInfo,
  });
  if (res?.code === SUCCESS_CODE) return { success: true, data: res.data, message: '更新成功' };
  return { success: false, data: res?.data, message: res?.message || '更新失败' };
}

export async function removeDingRobot(id: string) {
  const res: ResponseData<null> = await request(`/api/system-warning/v1.0.0/robot/${id}`, {
    method: 'DELETE',
  });
  if (res?.code === SUCCESS_CODE) return { success: true, message: '删除成功' };
  return { success: false, message: res?.message || '删除失败' };
}

export async function queryDingRobotNames(name: string) {
  const res: ResponseData<OptionTypes[]> = await request('/api/system-warning/v1.0.0/robot-names', {
    params: { name },
  });
  if (res?.code === SUCCESS_CODE) {
    return res.data;
  }
  return [];
}

export async function querySystemWarning(params: any, sort: any) {
  const res: ResponseData<{
    total: number;
    list: SystemWarningRuleTableItem[];
  }> = await request('/api/system-warning/v1.0.0/list', {
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.list };
  return { success: false, total: 0, data: [] };
}

export async function querySystemWarningHistory(params: any, sort: any) {
  const res: ResponseData<{
    total: number;
    list: SystemWarningHistroyTableItem[];
  }> = await request('/api/system-warning/v1.0.0/histories', {
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.list };
  return { success: false, total: 0, data: [] };
}

export async function createSystemWarningRule(params: CreateSystemWarningRule) {
  const res: ResponseData<null> = await request('/api/system-warning/v1.0.0/rule', {
    method: 'POST',
    data: params,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '添加成功',
    };
  return {
    data: res.data,
    success: false,
    message: res?.message || '添加失败',
  };
}

export async function updateSystemWarningRule(id: string, newInfo: UpdateSystemWarningRule) {
  const res: ResponseData<null> = await request(`/api/system-warning/v1.0.0/rule/${id}`, {
    method: 'PUT',
    data: newInfo,
  });
  if (res?.code === SUCCESS_CODE) return { success: true, data: res.data, message: '更新成功' };
  return { success: false, data: res?.data, message: res?.message || '更新失败' };
}

export async function removeSystemWarningRule(id: string) {
  const res: ResponseData<null> = await request(`/api/system-warning/v1.0.0/rule/${id}`, {
    method: 'DELETE',
  });
  if (res?.code === SUCCESS_CODE) return { success: true, message: '删除成功' };
  return { success: false, message: res?.message || '删除失败' };
}
